package com.lbj.mall.product.controller;

import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.product.entity.AttrEntity;
import com.lbj.mall.product.entity.AttrGroupEntity;
import com.lbj.mall.product.entity.vo.AttrIdAndAttrGroupIdVo;
import com.lbj.mall.product.entity.vo.AttrWithGroupVo;
import com.lbj.mall.product.service.AttrAttrgroupRelationService;
import com.lbj.mall.product.service.AttrGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author libingjun
 * @email 617416791@qq.com
 * @date 2022-03-27 10:44:55
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表 根据id来查询，如果 catelogId 为0，则查询全部
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        //AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 这里需要根据id查出当前的数据，并查出该id对应的所有的父类字段
        AttrGroupEntity attrGroup = attrGroupService.getNewDataById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("{attrgroupId}/attr/relation")
    public R getAttrFromRelation(@PathVariable("attrgroupId") Long attrGroupId) {
        List<AttrEntity> data = attrGroupService.getAttrFromRelation(attrGroupId);
        return R.ok().put("data", data);
    }

    /**
     * 删除中间表：属性与属性分组的中间表数据
     * <p>
     * RequestBody:从请求体中获取数据
     */
    @PostMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrIdAndAttrGroupIdVo> list) {
        attrGroupService.deleteAttrRelation(list);
        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getAttrGroupNoAttrList(@RequestParam Map<String, Object> params,
                                    @PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils pageUtils = attrGroupService.getAttrGroupNoAttrList(params, attrGroupId);
        return R.ok().put("data", pageUtils);
    }

    /**
     * 添加属性与属性分组关联
     */
    @PostMapping("attr/relation")
    public R setAttrRelation(@RequestBody List<AttrIdAndAttrGroupIdVo> list) {
        relationService.setAttrRelation(list);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("{catelogId}/withattr")
    public R attrWithAttrGroup(@PathVariable("catelogId") Long categoryId) {
        List<AttrWithGroupVo> data = attrGroupService.getAttrWithGroupByCategoryId(categoryId);
        return R.ok().put("data", data);
    }
}
